package com.zuxia.impDao;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zuxia.iDao.InfotypeinfoDAOI;
import com.zuxia.po.Infocenterinfo;
import com.zuxia.po.Infotypeinfo;
import com.zuxia.po.Partstypeinfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Infotypeinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.zuxia.impDao.Infotypeinfo
 * @author MyEclipse Persistence Tools
 */

public class InfotypeinfoDAO extends HibernateDaoSupport implements InfotypeinfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(InfotypeinfoDAO.class);
	// property constants
	public static final String ITITYPE = "ititype";

	protected void initDao() {
		// do nothing
	}

	public void save(Infotypeinfo transientInstance) {
		log.debug("saving Infotypeinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public Boolean delete(Infotypeinfo persistentInstance) {
		log.debug("deleting Infotypeinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
		return null;
	}

	public Infotypeinfo findById(java.lang.Integer id) {
		log.debug("getting Infotypeinfo instance with id: " + id);
		try {
			Infotypeinfo instance = (Infotypeinfo) getHibernateTemplate().get(
					"com.zuxia.po.Infotypeinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Infotypeinfo instance) {
		log.debug("finding Infotypeinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfotypeinfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Infotypeinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Infotypeinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfotypeinfoDAOI#findByItitype(java.lang.Object)
	 */
	public List findByItitype(Object ititype) {
		return findByProperty(ITITYPE, ititype);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InfotypeinfoDAOI#findAll()
	 */
	public Infotypeinfo merge(Infotypeinfo detachedInstance) {
		log.debug("merging Infotypeinfo instance");
		try {
			Infotypeinfo result = (Infotypeinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Infotypeinfo instance) {
		log.debug("attaching dirty Infotypeinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Infotypeinfo instance) {
		log.debug("attaching clean Infotypeinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static InfotypeinfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (InfotypeinfoDAOI) ctx.getBean("InfotypeinfoDAO");
	}
//添加类型
	public Boolean addTyep(String Type) {
		Infotypeinfo info= new Infotypeinfo();
		info.setItiid(null);
		info.setItitype(Type);
		getHibernateTemplate().saveOrUpdate(info);
		return true;
	}
	//查询全部类型
	public List findAll() {
		log.debug("finding all Infotypeinfo instances");
		try {
			String queryString = "from Infotypeinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	//添加新闻的方法
	public Boolean addCenter(String centerName, int centerType,Date centerTime, String centerText) {
		Infocenterinfo center =new Infocenterinfo();
		Infotypeinfo info=new Infotypeinfo();
		info.setItiid(centerType);
			center.setIciid(null);
			center.setIcititle(centerName);
			center.setIcitime(centerTime);
			center.setIcicontext(centerText);
			center.setInfotypeinfo(info);
			getHibernateTemplate().saveOrUpdate(center);
		return true;
		
	}

	public Infotypeinfo idShowInfotypeinfo(int itiid) {
		// TODO Auto-generated method stub
		log.debug("from Infotypeinfo where itiid =" + itiid);
		try {
			return getHibernateTemplate().get(Infotypeinfo.class, itiid);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

}